import datetime
import sys
from common.Uilt import forum_driver
import time

from selenium import webdriver
from selenium.webdriver import Keys
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager

class LogOn:
    driver = ""

    def __init__(self):
        self.driver = forum_driver.driver
        self.driver.get("http://127.0.0.1:58080/sign-in.html")

#成功登陆的测试用例
    def LoginSuc(self):
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()

        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("asd")
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("123")
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()

        test = self.driver.find_element(By.CSS_SELECTOR, "#nav_board_index > a > span.nav-link-title").text
        assert test == "首页" #如果断言失败则登录失败
        forum_driver.getwinshot()
        self.driver.back()
    def Loginfail(self):
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()

        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("asd")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("321asd")#错误的密码
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()

        test = self.driver.find_element(By.CSS_SELECTOR, "body > div.jq-toast-wrap.bottom-right > div").get_attribute("style")
        #检查是否失败
        assert test != "text-align: left; display: none;"
        forum_driver.getwinshot()
        self.driver.quit()

login = LogOn()
login.LoginSuc()
login.Loginfail()
